package com.ruoyi.hikvision.hk;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.hikvision.NetSDK.HCNetSDK;
import com.sun.jna.Pointer;

public class HikvisionAlarmCallBack implements HCNetSDK.FMSGCallBack_V31 {

    private String sIP;
    private String deviceNo;
    private HikvisionDataLogic dataLogic;

    public void setsIP(String ip) {
        this.sIP = ip;
    }

    public void setDeviceNo(String deviceNo) {
        this.deviceNo = deviceNo;
    }

    public HikvisionDataLogic getDataLogic() {
        //if (dataLogic == null) dataLogic = AppContext.getBean(HikvisionDataLogic.class);
        if (dataLogic == null) dataLogic = SpringUtils.getBean(HikvisionDataLogic.class);
        return dataLogic;
    }

    @Override
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        // 门禁主机报警信息，通过主次类型的可以判断当前的具体门禁类型，例如（主类型0x5 次类型0x4b 表示人脸认证通过，主类型0x5 次类型0x17 表示人脸认证失败）// 75-人脸认证通过，21-门锁打开
        if (lCommand == HCNetSDK.COMM_ALARM_ACS) {
            HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
            strACSInfo.write();
            Pointer pACSInfo = strACSInfo.getPointer();
            pACSInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size());
            strACSInfo.read();
            // 时间解析
            String strTime = strACSInfo.struTime.dwYear + "-" + strACSInfo.struTime.dwMonth + "-" + strACSInfo.struTime.dwDay + " " + strACSInfo.struTime.dwHour + ":" + strACSInfo.struTime.dwMinute + ":" + strACSInfo.struTime.dwSecond;
            // 工号解析
            HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND strExtendInfo = new HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND();
            strExtendInfo.write();
            Pointer pExtendInfo = strExtendInfo.getPointer();
            pExtendInfo.write(0, strACSInfo.pAcsEventInfoExtend.getByteArray(0, strExtendInfo.size()), 0, strExtendInfo.size());
            strExtendInfo.read();
            String employeeNo = new String(strExtendInfo.byEmployeeNo).trim();
            System.out.println("[" + sIP + "] [" + deviceNo + "] 事件回调 lCommand:" + lCommand + ", dwMajor:" + strACSInfo.dwMajor + ", dwMinor:" + strACSInfo.dwMinor + ", time:" + strTime + ", employeeNo:" + employeeNo);
            // 人脸认证通过数据处理
            if (strACSInfo.dwMinor == 75) savePickRecord(deviceNo, employeeNo, strTime);
        }
        return true;
    }

    public void savePickRecord(String deviceNo, String employeeNo, String strTime) {
        if (getDataLogic() == null) return;
        dataLogic.savePickRecord(deviceNo, employeeNo, strTime);
    }

}
